<?php
namespace lib;
use lib\Curl;
use think\Cache;
//发送微信模板消息类
class WechatNews{
	public $appid    ='wxd0465d5af41c5968';
	public $AppSecret='3f7c7c50876a032127520f80587b32fa';			//公众号秘钥
	//获取微信全局access_token
	public function access_token(){
		$access_token=Cache::get('access_token');
		if(!$access_token){
			$rel=$this->get_access_token();
			Cache::set('access_token',$rel['access_token'],7000);
			return $rel['access_token'];
		}else{
			return $access_token; 
		}
	}
	//获取access_token及获取时间expires_in
	protected function get_access_token(){
		$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->AppSecret;
		$rel=Curl::https_curl($url);
		$rel=json_decode($rel,1);
		return $rel;
	}
	//发送模板消息
	public function post_msg($data){
		$data=json_encode($data);
		$access_token=$this->access_token();
		$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
		$rel=Curl::https_curl($url,$data);
		$rel=json_decode($rel,true);
	}
}
